package com.rad.datastat.database;

import java.util.concurrent.locks.ReentrantLock;

import com.rad.datastat.model.AppMd5Info;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

public class AppMd5DB {
	public static final String TABLE_NAME = "AppMd5";

	public static final String COL_ID = "_id";
	public static final String COL_PKG_NAME = "pkg_name";
	public static final String COL_APP_NAME = "app_name";
	public static final String COL_MD5 = "md5";

	private static AppMd5DB mInstance;
	private DBOpenHelper mDBOpenHelper;
	private ReentrantLock mLock;

	public AppMd5DB(Context context) {
		mDBOpenHelper = DBOpenHelper.instance(context);
		mLock = mDBOpenHelper.getLock();
	}

	public static synchronized AppMd5DB instance(Context context) {
		if (mInstance == null) {
			mInstance = new AppMd5DB(context);
		}

		return mInstance;
	}

	protected static void createTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + COL_ID
				+ " integer primary key autoincrement, " + COL_PKG_NAME
				+ " TEXT, " + COL_APP_NAME + " TEXT, " + COL_MD5 + " TEXT) ");
	}

	// 保存apk的md5
	public boolean save(AppMd5Info info) {
		if(info == null){
			return false;
		}
		String pkgName = info.getPkgName();
		String appName = info.getAppName();
		String md5 = info.getMd5();
		if (TextUtils.isEmpty(pkgName) || TextUtils.isEmpty(appName)
				|| TextUtils.isEmpty(md5)) {
			return false;
		}

		if (exits(pkgName)) {
			return update(pkgName, appName, md5);
		} else {
			return insert(pkgName, appName, md5);
		}
	}

	// 判断包名是否存在
	public boolean exits(String pkgName) {
		SQLiteDatabase db = null;
		Cursor cur = null;
		try {
			mLock.lock();
			db = mDBOpenHelper.getReadableDatabase();

			String selection = COL_PKG_NAME + "=?";
			String[] selectionArgs = new String[] { pkgName };
			cur = db.query(TABLE_NAME, null, selection, selectionArgs, null,
					null, null);

			return cur.moveToFirst();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}

	// 插入一条数据
	public boolean insert(String pkgName, String appName, String md5) {
		SQLiteDatabase db = null;
		try {
			mLock.lock();
			db = mDBOpenHelper.getWritableDatabase();

			ContentValues values = new ContentValues();
			values.put(COL_PKG_NAME, pkgName);
			values.put(COL_APP_NAME, appName);
			values.put(COL_MD5, md5);

			long ret = db.insert(TABLE_NAME, null, values);

			return ret != -1;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}

	// 更新一条数据
	public boolean update(String pkgName, String appName, String md5) {
		SQLiteDatabase db = null;
		Cursor cur = null;
		try {
			mLock.lock();
			db = mDBOpenHelper.getWritableDatabase();

			ContentValues values = new ContentValues();
			values.put(COL_APP_NAME, appName);
			values.put(COL_MD5, md5);
			String whereClause = COL_PKG_NAME + "=?";
			String[] whereArgs = new String[] { pkgName };

			int ret = db.update(TABLE_NAME, values, whereClause, whereArgs);

			return ret != 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}
	
	public AppMd5Info queryByPkgName(String pkgName){
		SQLiteDatabase db = null;
		Cursor cur = null;
		try {
			mLock.lock();
			db = mDBOpenHelper.getReadableDatabase();
			String selection = COL_PKG_NAME + "=?";
			String[] selectionArgs = new String[] { pkgName };

			cur = db.query(TABLE_NAME, null, selection, selectionArgs, null,
					null, null);
			
			if (cur.moveToFirst()) {
				AppMd5Info info = new AppMd5Info();
				info.setPkgName(cur.getString(cur.getColumnIndex(COL_PKG_NAME)));
				info.setAppName(cur.getString(cur.getColumnIndex(COL_APP_NAME)));
				info.setMd5(cur.getString(cur.getColumnIndex(COL_MD5)));
				return info;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}

		return null;
	}
}
